<script setup lang="ts">
  import router from './router';
  import { useUserInfoStoreWithOut } from './stores/modules/userInfo';
  const { getToken } = storeToRefs(useUserInfoStoreWithOut());

  router.beforeEach((to, form, next) => {
    if (to.meta.title) {
      document.title = to.meta.title as string;
    }

    if (getToken.value && to.name !== 'startExam' && form.name === 'startExam' && form.params.id) {
      router.replace({ name: form.name as string, params: { id: form.params.id } });
      return;
    }
    if ((to.name === 'exam' || to.name === 'home') && !getToken.value) {
      next('/login');
      return;
    }
    if (to.name === 'login' && getToken.value) {
      next('/home');
      return;
    }
    next();
  });
</script>

<template>
  <router-view />
</template>

<style>
  html,
  body,
  #app {
    width: 100%;
    height: 100%;
  }
</style>
